package com.android.broad;

import static com.android.broad.Constants.LOG_TAG;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.broad.domain.Event;

public class Service extends android.app.Service {

  private static Service instance;
  private static boolean running;

  public static boolean checkPermissions(final Context context,
      final String[] neededPermissions) {
    for (final String permission : neededPermissions)
      if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        Log.e(LOG_TAG, "security error : requires " + permission
            + " permission");
        return false;
      }
    return true;
  }

  public static Service getInstance() {
    return instance;
  }

  public static boolean isEnabled(final Context context) {
    final SharedPreferences prefs = context.getSharedPreferences(
        MainBroadCast.class.getSimpleName(), Context.MODE_PRIVATE);
    return prefs.getBoolean("serviceEnabled", false);
  }

  public static boolean isRunning() {
    return running;
  }

  private static void setEnabled(final Context context,
      final boolean serviceEnabled) {
    final SharedPreferences prefs = context.getSharedPreferences(
        MainBroadCast.class.getSimpleName(), Context.MODE_PRIVATE);
    final Editor editor = prefs.edit();
    editor.putBoolean("serviceEnabled", serviceEnabled);
    editor.commit();
  }

  public static void start(final Context context, final boolean memorize) {
    if (memorize)
      setEnabled(context, true);
    context.startService(new Intent(context, Service.class));
  }

  public static void stop(final Context context, final boolean memorize) {
    if (memorize)
      setEnabled(context, false);
    context.stopService(new Intent(context, Service.class));
  }

  //private Database db;
  private String androidDeviceId;

  public Service() {
    super();
    Log.v(LOG_TAG, "creating service");
    instance = this;
  }

  public boolean acceptIncomingEvent(final Event event) {
    if (!running || event == null)
      return false;
    if (androidDeviceId == null) {
      final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
      androidDeviceId = telephonyManager.getSubscriberId();
    }
    event.deviceId = androidDeviceId != null ? androidDeviceId : "Unknown";
    if (event.timestamp == 0)
      event.timestamp = System.currentTimeMillis();
    //notifyEvent(event);
    return true;

  }

  @Override
  public IBinder onBind(final Intent intent) {
    Log.v(LOG_TAG, "onBind(" + intent + ")");
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    startService();
  }

  @Override
  public void onDestroy() {
    shutdownService();
    super.onDestroy();
  }


  private void shutdownService() {
    Log.v(LOG_TAG, "ending service");
    running = false;
    //db = null;

  }

  private void startService() {
    Log.v(LOG_TAG, "starting service");
    //db = new Database(this);
    running = true;
    //Intent i = new Intent(this, Rootme.class);
    //startActivity(i);
  }

}